package com.bianguo.android.tencentpushdemo.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bianguo.android.tencentpushdemo.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/23.
 */

public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private ArrayList<String> list;
    private LayoutInflater inflater;
    private OnClickItemListener onClickItemListener;

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public CommentAdapter(Context mContext, ArrayList<String> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (0 == viewType) {
            View view = inflater.inflate(R.layout.item_add_raiders_layout, parent, false);
            return new AddViewHolder(view);
        } else if (1 == viewType) {
            View view = inflater.inflate(R.layout.item_home_layout, parent, false);
            return new CommentViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                AddViewHolder addViewHolder = (AddViewHolder) holder;
                addViewHolder.addRaiders.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, holder.getAdapterPosition());
                        }
                    }
                });
                break;
            case 1:
                CommentViewHolder commentViewHolder = (CommentViewHolder) holder;
                String s = list.get(position - 1);
                commentViewHolder.homeTextTv.setText(s);
                commentViewHolder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, holder.getAdapterPosition());
                        }
                    }
                });
                commentViewHolder.homeTextTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, holder.getAdapterPosition());
                        }
                    }
                });
                commentViewHolder.addRaiders.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClickItemListener != null) {
                            onClickItemListener.onClickItem(v, holder.getAdapterPosition());
                        }
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() + 1 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (0 == position) {
            return 0;
        } else {
            return 1;
        }
    }

    public static class CommentViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_home_text)
        TextView homeTextTv;
        @BindView(R.id.item_home_add_raiders)
        ImageView addRaiders;
        @BindView(R.id.item_home_image)
        ImageView image;

        public CommentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class AddViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_add_raiders)
        ImageView addRaiders;

        public AddViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClickItemListener{
        void onClickItem(View view, int position);
    }
}
